<template>
    <div>
        <el-dialog
            title="岗位系数设定"
            width="40%"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">

                    <el-row>
                        <el-col :span="8">
                            <el-date-picker
                                v-model="form.year"
                                value-format="yyyy"
                                :clearable="false"
                                :editable="false"
                                type="year"
                                clearable
                                style="width: 98%"
                                placeholder="年份">
                            </el-date-picker>
                        </el-col>
                        <el-col :span="8">
                            <el-input-number placeholder="系数"  v-model="form.coeff" :precision="2" :step="0.1" :max="10" clearable style="width: 98%"/>
                        </el-col>
                        <el-col :span="8">
                            <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
                        </el-col>
                    </el-row>

                    <el-table
                        class="mt10"
                        ref="table"
                        size="medium"
                        height="360"
                        empty-text="暂无数据"
                        element-loading-text="给我一点时间"
                        fit border highlight-current-row
                        :stripe="true"
                        v-loading="loading"
                        :data="dataList">

                        <el-table-column label="年份" prop="year" align="center"/>
                        <el-table-column label="系数" prop="coeff" align="center"/>
                        <el-table-column label="操作" prop="btn" width="200px" align="center">
                            <template slot-scope="scope">
                                <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

            </div>

        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {
    },
    data() {
        return {
            visible: false,
            loading: false,
            submitLoading: false,

            dataList: [],
            queryParams: {
                sortColumns: 'year',
                sortType: 'DESC',
                postId: null
            },
            form: {
                postId: null,
                year: new Date().getFullYear()+'',
                coeff: 1
            }

        }
    },

    methods: {
        init(id) {
            const refs = this.$refs
            this.form.postId = id
            this.queryParams.postId = id
            this.visible = true

            this.$nextTick(() => {
                this.getDataList(id)
            })
        },

        /**
         * 获取表格数据
         */
        async getDataList() {
            this.loading = true
            const res = await this.$http({
                url: '/hr/post_coefficient/list',
                method: 'GET',
                params: this.queryParams
            })
            if (this.ResultMessage(res, false)) {
                this.dataList = res.data || []
            }
            this.loading = false
        },

        /**
         * 删除
         */
        async handleDelete(row) {
            let ids = []
            if (row) {
                ids.push(row.id)
            }
            if (this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({title: '提示', message: '请选择需要删除的记录！', type: 'info'});
            } else {
                const confirm = await this.$confirm('确定要删除？', '提示', {type: 'warning'}).catch(() => {
                })
                if (confirm) {
                    const res = await this.$http({
                        url: '/hr/post_coefficient/delete',
                        method: 'POST',
                        data: ids
                    })
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                }
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            console.log(this.form);
            this.submitLoading = true
            const res = await this.$http({
                url: `/hr/post_coefficient/save`,
                method: 'POST',
                data: this.form
            })
            if (this.ResultMessage(res)) {
                this.getDataList()
                this.submitLoading = false
            } else {
                this.submitLoading = false
            }
        }
    }
};
</script>

<style lang="scss" scoped></style>
